﻿using System;
using System.IO;
using System.Windows.Forms;
using System.Xml.Linq;
using Styx;
using Styx.Common;

namespace MIBHunter
{
    public partial class MIBHunterGUI : Form
    {
        public MIBHunterGUI()
        {
            InitializeComponent();
        }

        #region Load

        private void MIBHunterGUI_Load(object sender, EventArgs e)
        {
            if (Classname.GetCurrentSpec() == "BeastMastery" && !StyxWoW.Me.Name.Contains("Neelo"))
            {
                tabControl1.TabPages.Remove(tabPage3);
                tabControl1.TabPages.Remove(tabPage4);
            }
            else if (Classname.GetCurrentSpec() == "Marksmanship" && !StyxWoW.Me.Name.Contains("Neelo"))
            {
                tabControl1.TabPages.Remove(tabPage2);
                tabControl1.TabPages.Remove(tabPage4);
            }
            else if (Classname.GetCurrentSpec() == "Survival" && !StyxWoW.Me.Name.Contains("Neelo"))
            {
                tabControl1.TabPages.Remove(tabPage2);
                tabControl1.TabPages.Remove(tabPage3);
            }

            AoEKey.SelectedIndex = MIBSettings.Instance.AoEKey;
            AttackOOC.Checked = MIBSettings.Instance.AttackOOC;
            AutoMove.Checked = MIBSettings.Instance.AutoMove;
            AutoFace.Checked = MIBSettings.Instance.AutoFace;
            AutoBuff.Checked = MIBSettings.Instance.AutoBuff;
            AutoRacial.Checked = MIBSettings.Instance.AutoRacial;
            AutoTarget.Checked = MIBSettings.Instance.AutoTarget;
            Backward.SelectedIndex = MIBSettings.Instance.Backward;
            BurstKey.SelectedIndex = MIBSettings.Instance.BurstKey;
            BurstHP.Value = MIBSettings.Instance.BurstHP;
            Forward.SelectedIndex = MIBSettings.Instance.Forward;
            HealthStone.Checked = MIBSettings.Instance.HealthStone;
            HealthStoneHP.Value = MIBSettings.Instance.HealthStoneHP;
            PauseKey.SelectedIndex = MIBSettings.Instance.PauseKey;
            ProfBuff.SelectedIndex = MIBSettings.Instance.ProfBuff;
            ProfBuffHP.Value = MIBSettings.Instance.ProfBuffHP;
            StartEating.Value = MIBSettings.Instance.StartEating;
            StrafleLeft.SelectedIndex = MIBSettings.Instance.StrafleLeft;
            StrafleRight.SelectedIndex = MIBSettings.Instance.StrafleRight;
            Trinket1.SelectedIndex = MIBSettings.Instance.Trinket1;
            Trinket1HP.Value = MIBSettings.Instance.Trinket1HP;
            Trinket2.SelectedIndex = MIBSettings.Instance.Trinket2;
            Trinket2HP.Value = MIBSettings.Instance.Trinket2HP;
            TurnLeft.SelectedIndex = MIBSettings.Instance.TurnLeft;
            TurnRight.SelectedIndex = MIBSettings.Instance.TurnRight;
            UseFoodDrink.Checked = MIBSettings.Instance.UseFoodDrink;
        }

        #endregion

        public string LastSavedMode = "";

        private void OK_Click(object sender, EventArgs e)
        {
            MIBSettings.Instance.LastSavedMode = LastSavedMode;
            MIBSettings.Instance.LastSavedSpec = Classname.GetCurrentSpec();
            MIBSettings.Instance.Save();
            Logging.Write("----------------------------------");
            Logging.Write("MIBHunter Settings wurden gespeichert");
            Close();

            MIBSettings.Instance.UpdateStatus = true;
        }

        private void Speichern_Click(object sender, EventArgs e)
        {
            MIBSettings.Instance.LastSavedMode = LastSavedMode;
            MIBSettings.Instance.LastSavedSpec = Classname.GetCurrentSpec();
            MIBSettings.Instance.Save();
            Logging.Write("MIBHunter Settings wurden gespeichert");
            MIBSettings.Instance.UpdateStatus = true;
        }

        private void Dungeon_Click(object sender, EventArgs e)
        {
            if (File.Exists(Utilities.AssemblyDirectory +
                @"\Routines\MIBHunter\Preset\Default Dungeon.xml"))
            {
                MessageBox.Show(
                    "Dungeon Mode: Work best with LazyRaider in Dungeon.\r\n\r\nMake sure ONLY 3 Options Enabled in LazyRaider:\r\n - Run Without a Tank\r\n - Disable Plug-ins\r\n - Frame Lock",
                    "Important Note",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);

                MIBSettings.Instance.LoadFromXML(XElement.Load(Utilities.AssemblyDirectory +
                                                              @"\Routines\MIBHunter\Preset\Default Dungeon.xml"));
                Logging.Write("----------------------------------");
                Logging.Write("Standardprofil für Dungeons geladen");
                Logging.Write("----------------------------------");
                MIBHunterGUI_Load(null, null);
            }
            else
            {
                Logging.Write("----------------------------------");
                Logging.Write("File not exists: " + Utilities.AssemblyDirectory +
                              @"\Routines\MIBHunter\Preset\Default Dungeon.xml");
                Logging.Write("Fehler beim Laden vom Dungen Standardprofil.");
                Logging.Write("----------------------------------");
                MIBHunterGUI_Load(null, null);
            }
        }

        private void Raid_Click(object sender, EventArgs e)
        {
            if (File.Exists(Utilities.AssemblyDirectory +
                @"\Routines\MIBHunter\Preset\Default Raid.xml"))
            {
                MessageBox.Show(
                    "Raid Mode: Work best with LazyRaider in Raiding.\r\n\r\nMake sure ONLY 3 Options Enabled in LazyRaider:\r\n - Run Without a Tank\r\n - Disable Plug-ins\r\n - Frame Lock",
                    "Important Note",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                MIBSettings.Instance.LoadFromXML(XElement.Load(Utilities.AssemblyDirectory +
                                                              @"\Routines\MIBHunter\Preset\Default Raid.xml"));
                Logging.Write("----------------------------------");
                Logging.Write("Standardprofil für Raid geladen");
                Logging.Write("----------------------------------");
                MIBHunterGUI_Load(null, null);
            }
            else
            {
                Logging.Write("----------------------------------");
                Logging.Write("File not exists: " + Utilities.AssemblyDirectory +
                              @"\Routines\MIBHunter\Preset\Default Raid.xml");
                Logging.Write("Fehler beim Laden vom Raid Standardprofil.");
                Logging.Write("----------------------------------");
            }
        }

        private void PvP_Click(object sender, EventArgs e)
        {
            if (File.Exists(Utilities.AssemblyDirectory +
                @"\Routines\MIBHunter\Preset\Default PvP.xml"))
            {
                MessageBox.Show(
                    "PvP Mode: Work best with LazyRaider in PvP.\r\n\r\nMake sure ONLY 3 Options Enabled in LazyRaider:\r\n - Run Without a Tank\r\n - Disable Plug-ins\r\n - Frame Lock",
                    "Important Note", MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
                MIBSettings.Instance.LoadFromXML(XElement.Load(Utilities.AssemblyDirectory +
                                                              @"\Routines\MIBHunter\Preset\Default PvP.xml"));
                Logging.Write("----------------------------------");
                Logging.Write("Standardprofil für PVP geladen");
                Logging.Write("----------------------------------");
                MIBHunterGUI_Load(null, null);
            }
            else
            {
                Logging.Write("----------------------------------");
                Logging.Write("File not exists: " + Utilities.AssemblyDirectory +
                              @"\Routines\MIBHunter\Preset\Default PvP.xml");
                Logging.Write("Fehler beim Laden vom PVP Standardprofil.");
                Logging.Write("----------------------------------");
            }
        }

        private void Quest_Click(object sender, EventArgs e)
        {
            if (File.Exists(Utilities.AssemblyDirectory +
                @"\Routines\MIBHunter\Preset\Default Quest.xml"))
            {
                MessageBox.Show(
                    "Quest Helper Mode: Work best with LazyRaider in solo questing.\r\n\r\nJust select your target, the bot will move to melee range and attack.\r\n\r\nMake sure ONLY 3 Options Enabled in LazyRaider:\r\n - Run Without a Tank\r\n - Disable Plug-ins\r\n - Frame Lock",
                    "Important Note", MessageBoxButtons.OK, MessageBoxIcon.Information);
                MIBSettings.Instance.LoadFromXML(XElement.Load(Utilities.AssemblyDirectory +
                                                              @"\Routines\MIBHunter\Preset\Default Quest.xml"));
                Logging.Write("----------------------------------");
                Logging.Write("Standardprofil für Quest geladen");
                Logging.Write("----------------------------------");
                MIBHunterGUI_Load(null, null);
            }
            else
            {
                Logging.Write("----------------------------------");
                Logging.Write("File not exists: " + Utilities.AssemblyDirectory +
                              @"\Routines\MIBHunter\Preset\Default Quest.xml");
                Logging.Write("Fehler beim Laden vom Quest Standardprofil.");
                Logging.Write("----------------------------------");
            }
        }

        private void AFK_Click(object sender, EventArgs e)
        {
            if (File.Exists(Utilities.AssemblyDirectory +
                @"\Routines\MIBHunter\Preset\Default AFK.xml"))
            {
                MessageBox.Show(
                    "Full AFK Mode: Work best with BGBuddy, ArcheologyBuddy, DungeonBuddy, Grind Bot, Questing...",
                    "Important Note", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Logging.Write("----------------------------------");
                MIBSettings.Instance.LoadFromXML(XElement.Load(Utilities.AssemblyDirectory +
                                                              @"\Routines\MIBHunter\Preset\Default AFK.xml"));
                Logging.Write("Standardprofil für Full AFK geladen");
                Logging.Write("----------------------------------");
                MIBHunterGUI_Load(null, null);
            }
            else
            {
                Logging.Write("----------------------------------");
                Logging.Write("File not exists: " + Utilities.AssemblyDirectory +
                              @"\Routines\MIBHunter\Preset\Default AFK.xml");
                Logging.Write("Fehler beim Laden vom Full AFK Standardprofil.");
                Logging.Write("----------------------------------");
            }
        }

        private void LoadSetting_Click(object sender, EventArgs e)
        {
            var openFileDialog = new OpenFileDialog
            {
                Filter = "Setting File|*.xml",
                Title = "Lade Settings von Datei",
                InitialDirectory = Utilities.AssemblyDirectory + @"\Routines\MIBHunter\User Settings\"
            };
            openFileDialog.ShowDialog();

            if (openFileDialog.FileName.Contains(".xml"))
            {
                MIBSettings.Instance.LoadFromXML(XElement.Load(openFileDialog.FileName));
                Logging.Write("----------------------------------");
                Logging.Write("Lade Settings von: " + openFileDialog.FileName);
                Logging.Write("----------------------------------");
                MIBHunterGUI_Load(null, null);
            }
            else
            {
                return;
            }
        }

        private void SaveSetting_Click(object sender, EventArgs e)
        {
            var saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Setting File|*.xml";
            saveFileDialog.Title = "Speichere Settings in Datei";
            saveFileDialog.InitialDirectory = Utilities.AssemblyDirectory + @"\Routines\MIBHunter\User Settings\";
            saveFileDialog.DefaultExt = "xml";
            saveFileDialog.FileName = "MIBHunter 1.0 " + Classname.CurrentSpec;

            saveFileDialog.ShowDialog();

            if (saveFileDialog.FileName.Contains(".xml"))
            {
                MIBSettings.Instance.SaveToFile(saveFileDialog.FileName);
                Logging.Write("----------------------------------");
                Logging.Write("Speichere Setting in: " + saveFileDialog.FileName);
                Logging.Write("----------------------------------");
            }
            else
            {
                return;
            }
        }

        private void BurstKey_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (var i = 0; i <= 46; i++)
            {
                if (BurstKey.SelectedIndex == i)
                {
                    MIBSettings.Instance.BurstKey = i;
                }
            }
        }

        private void ProfBuff_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (var i = 0; i <= 8; i++)
            {
                if (ProfBuff.SelectedIndex == i)
                {
                    MIBSettings.Instance.ProfBuff = i;
                }
            }
        }

        private void Trinket1_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (var i = 0; i <= 8; i++)
            {
                if (Trinket1.SelectedIndex == i)
                {
                    MIBSettings.Instance.Trinket1 = i;
                }
            }
        }

        private void Trinket2_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (var i = 0; i <= 8; i++)
            {
                if (Trinket2.SelectedIndex == i)
                {
                    MIBSettings.Instance.Trinket2 = i;
                }
            }
        }

        private void BurstHP_ValueChanged(object sender, EventArgs e)
        {
            MIBSettings.Instance.BurstHP = (int)BurstHP.Value;
        }

        private void ProfBuffHP_ValueChanged(object sender, EventArgs e)
        {
            MIBSettings.Instance.ProfBuffHP = (int)ProfBuffHP.Value;
        }

        private void Trinket1HP_ValueChanged(object sender, EventArgs e)
        {
            MIBSettings.Instance.Trinket1HP = (int)Trinket1HP.Value;
        }

        private void Trinket2HP_ValueChanged(object sender, EventArgs e)
        {
            MIBSettings.Instance.Trinket2HP = (int)Trinket2HP.Value;
        }

        private void StrafleLeft_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (var i = 0; i <= 40; i++)
            {
                if (StrafleLeft.SelectedIndex == i)
                {
                    MIBSettings.Instance.StrafleLeft = i;
                }
            }
        }

        private void Forward_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (var i = 0; i <= 40; i++)
            {
                if (Forward.SelectedIndex == i)
                {
                    MIBSettings.Instance.Forward = i;
                }
            }
        }

        private void StrafleRight_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (var i = 0; i <= 40; i++)
            {
                if (StrafleRight.SelectedIndex == i)
                {
                    MIBSettings.Instance.StrafleRight = i;
                }
            }
        }

        private void TurnLeft_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (var i = 0; i <= 40; i++)
            {
                if (TurnLeft.SelectedIndex == i)
                {
                    MIBSettings.Instance.TurnLeft = i;
                }
            }
        }

        private void Backward_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (var i = 0; i <= 40; i++)
            {
                if (Backward.SelectedIndex == i)
                {
                    MIBSettings.Instance.Backward = i;
                }
            }
        }

        private void TurnRight_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (var i = 0; i <= 40; i++)
            {
                if (TurnRight.SelectedIndex == i)
                {
                    MIBSettings.Instance.TurnRight = i;
                }
            }
        }

        private void AttackOOC_CheckedChanged(object sender, EventArgs e)
        {
            MIBSettings.Instance.AttackOOC = AttackOOC.Checked;
        }

        private void AutoBuff_CheckedChanged(object sender, EventArgs e)
        {
            MIBSettings.Instance.AutoBuff = AutoBuff.Checked;
        }

        private void AutoFace_CheckedChanged(object sender, EventArgs e)
        {
            MIBSettings.Instance.AutoFace = AutoFace.Checked;
        }

        private void AutoMove_CheckedChanged(object sender, EventArgs e)
        {
            MIBSettings.Instance.AutoMove = AutoMove.Checked;
        }

        private void AutoRacial_CheckedChanged(object sender, EventArgs e)
        {
            MIBSettings.Instance.AutoRacial = AutoRacial.Checked;
        }

        private void AutoTarget_CheckedChanged(object sender, EventArgs e)
        {
            MIBSettings.Instance.AutoTarget = AutoTarget.Checked;
        }

        private void UseFoodDrink_CheckedChanged(object sender, EventArgs e)
        {
            MIBSettings.Instance.UseFoodDrink = UseFoodDrink.Checked;
        }

        private void StartEating_ValueChanged(object sender, EventArgs e)
        {
            MIBSettings.Instance.StartEating = (int)StartEating.Value;
        }

        private void PauseKey_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (var i = 0; i <= 40; i++)
            {
                if (PauseKey.SelectedIndex == i)
                {
                    MIBSettings.Instance.PauseKey = i;
                }
            }
        }

        private void AoEKey_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (var i = 0; i <= 40; i++)
            {
                if (AoEKey.SelectedIndex == i)
                {
                    MIBSettings.Instance.AoEKey = i;
                }
            }
        }

        private void HealthStone_CheckedChanged(object sender, EventArgs e)
        {
            MIBSettings.Instance.HealthStone = HealthStone.Checked;
        }

        private void HealthStoneHP_ValueChanged(object sender, EventArgs e)
        {
            MIBSettings.Instance.HealthStoneHP = (int)HealthStoneHP.Value;
        }
    }
}
